package com.kyzh.core.pager.weal.accountbuy

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.util.AView
import com.gushenge.core.base.Base
import com.gushenge.core.beans.Deal
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.activities.BaseFragmentActivity
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.ItemDeallistTopItemBinding
import com.kyzh.core.databinding.ItemImagesBinding
import com.kyzh.core.dialog.shouListWindow
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.onLoginClick
import com.kyzh.core.utils.startActivity
import com.makeramen.roundedimageview.RoundedImageView
import kotlinx.android.synthetic.main.activity_deal_v3.*
import kotlinx.android.synthetic.main.activity_gift.*

class DealActivity : BaseActivity() {
    private var sort = 0
    private var system =0
    private var keyword = ""
    private var p =1
    private var max =1
    private val top = arrayListOf<Deal>()
    private val beans = arrayListOf<Deal>()
    private val adapter = Adapter(beans)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deal_v3)
        AView.setStatusBar(this,false)
        rvList.adapter =adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        srlRefresh.setOnRefreshListener {
            refresh()
            srlRefresh.isRefreshing = false
        }
    }

    private fun refresh() {
        WealRequest.dealList(sort,system,1,keyword){
            this@DealActivity.top.clear()
            beans.clear()
            this@DealActivity.top.addAll(top)
            beans.add(Deal(itemType1 = 0))
            beans.addAll(list)
            this@DealActivity.p = p
            this@DealActivity.max = max_p
            adapter.notifyDataSetChanged()
        }
    }
    private fun getData() {
        WealRequest.dealList(sort,system,p,keyword){
            this@DealActivity.p = p
            this@DealActivity.max = max_p
            adapter.addData(list)
        }
    }
    private inner class Adapter(beans:ArrayList<Deal>):BaseMultiItemQuickAdapter<Deal,BaseViewHolder>(beans),LoadMoreModule{

        init {
            addItemType(0,R.layout.item_deallist_top)
            addItemType(1,R.layout.item_deallist)
        }
        @SuppressLint("StringFormatMatches")
        override fun convert(holder: BaseViewHolder, item: Deal) {
            when(holder.itemViewType){
                0->{
                    holder.getView<TextView>(R.id.btSold).onLoginClick(this@DealActivity) {
                        startActivity<SmallActivity>()
                    }
                    holder.getView<TextView>(R.id.btHistory).onLoginClick(this@DealActivity) {
                        startActivity<BaseFragmentActivity>(GlobalKeys.TYPE to BaseFragmentActivity.MY_DEAL)
                    }
                    holder.getView<TextView>(R.id.btDealKnow).setOnClickListener {
                        startActivity<BrowserActivity>(GlobalKeys.TITLE to getString(R.string.dealKnow),GlobalKeys.LINK to Base.BASEURL+"?ct=trade&ac=Notice&app=1")
                    }
                    holder.getView<TextView>(R.id.btService).onLoginClick(this@DealActivity) {
                        startActivity<BrowserActivity>(GlobalKeys.LINK to "${Base.BASEURLV3}/?ct=app&ac=kefu&token=${MMKVConsts.uid}&member_id=${MMKVConsts.sub}",GlobalKeys.TITLE to getString(R.string.serviceCenter ))
                    }
                    val recyclerView = holder.getView<RecyclerView>(R.id.rvTop)
                    recyclerView.layoutManager = LinearLayoutManager(this@DealActivity,RecyclerView.HORIZONTAL,false)
                    recyclerView.adapter = TopAdapter(R.layout.item_deallist_top_item,top)
                    val tvSort = holder.getView<TextView>(R.id.tvSort)
                    tvSort.setOnClickListener {
                        val list = arrayListOf(getString(R.string.recentlyRelease),getString(R.string.priceAsc),getString(R.string.priceDesc))
                        val window = shouListWindow(list){
                            sort = this
                            tvSort.text = list[this]
                            refresh()
                        }

                        window.showAsDropDown(
                                tvSort,
                                0,
                                dip(10)
                        )
                    }
                    val etSearch = holder.getView<EditText>(R.id.etSearch)
                    val ivClose = holder.getView<ImageView>(R.id.ivClose)

                    etSearch.setOnEditorActionListener { v, actionId, event ->

                        keyword = etSearch.text.toString()
                        refresh()
                        return@setOnEditorActionListener true
                    }
                    ivClose.setOnClickListener {
                        keyword = ""
                        etSearch.setText("")
                        refresh()
                    }
                }
                1->{
                    holder.setText(R.id.tvName,item.name)
                            .setText(R.id.tvServer,item.sname)
                            .setText(R.id.tvTime,"${getString(R.string.postOn)} ${item.create_time}")
                            .setText(R.id.tvMoney,"¥${item.money}")
                            .setText(R.id.tvAllMoney, String.format(getString(R.string.cumulativeRecharge,item.pay_money)))
                            .setGone(R.id.rvImages,item.imgs.isEmpty())
                    holder.getView<RoundedImageView>(R.id.ivIcon).loadImage(item.icon)

                    val recyclerView = holder.getView<RecyclerView>(R.id.rvImages)
                    recyclerView.layoutManager = object :GridLayoutManager(this@DealActivity,3){
                        override fun canScrollHorizontally(): Boolean {
                            return false
                        }

                        override fun canScrollVertically(): Boolean {
                            return false
                        }
                    }
                    recyclerView.adapter = ImageAdapter(R.layout.item_images,item.imgs)

                    holder.itemView.setOnClickListener {
                        startActivity<BaseFragmentActivity>(
                                GlobalKeys.TYPE to BaseFragmentActivity.DEAL_PRODUCT_DETAIL,
                                GlobalKeys.KEY to item.id
                        )

                    }
                    recyclerView.setOnClickListener {
                        startActivity<BaseFragmentActivity>(
                                GlobalKeys.TYPE to BaseFragmentActivity.DEAL_PRODUCT_DETAIL,
                                GlobalKeys.KEY to item.id
                        )

                    }
                }
            }
        }
    }

    private inner class TopAdapter(layout:Int,beans:ArrayList<Deal>):BaseQuickAdapter<Deal,BaseDataBindingHolder<ItemDeallistTopItemBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemDeallistTopItemBinding>, item: Deal) {
            holder.dataBinding?.let {
                it.data = item
            }
            holder.itemView.setOnClickListener {
                startActivity<BaseFragmentActivity>(
                        GlobalKeys.TYPE to BaseFragmentActivity.DEAL_PRODUCT_DETAIL,
                        GlobalKeys.KEY to item.id
                )

            }
        }
    }
    private inner class ImageAdapter(layout:Int,beans:ArrayList<String>):BaseQuickAdapter<String,BaseDataBindingHolder<ItemImagesBinding>>(layout,beans){
        override fun convert(holder: BaseDataBindingHolder<ItemImagesBinding>, item: String) {
            holder.dataBinding?.let {
                it.data = item
            }
        }
    }
}